namespace Adge.Common
{    
    using System;
    using System.Data;
    using System.Web.Caching;

    public class CacheLogVisitor : LogVisitor
    {
        private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        
        static CacheLogVisitor()
        {
            CacheLogVisitor.syncObj = new object();
            lock (CacheLogVisitor.syncObj)
            {
                LogVisitor.m_IsWriteCachelog = ConfigFactory.GetConfig().Cachelog;
            }
        }

        public override void WriteLog(DefaultCacheStrategy dcs, string key, object val, CacheItemRemovedReason reason)
        {
            if (LogVisitor.m_IsWriteCachelog == 1)
            {
                string strReason = null;
                switch (reason)
                {
                    case CacheItemRemovedReason.Removed:
                        strReason = "\u7f13\u5b58\u88ab\u79fb\u9664(Removed)";
                        break;

                    case CacheItemRemovedReason.Expired:
                        strReason = "\u7f13\u5b58\u5df2\u5230\u671f(Expired)";
                        break;

                    case CacheItemRemovedReason.Underused:
                        strReason = "\u7cfb\u7edf\u91ca\u653e\u5185\u5b58(Underused)";
                        break;

                    case CacheItemRemovedReason.DependencyChanged:
                        strReason = "\u4f9d\u6574\u9879\u88ab\u4fee\u6539(DependencyChanged)";
                        break;
                }
                string logInfo = string.Format("([cacheaction] ={0}; [actioncontext] = {1})", strReason, val.ToString());
                log.Info(logInfo);
                //string sql = string.Format("INSERT INTO [" + BaseConfigFactory.GetTablePrefix + "cachelog] ([cacheaction],[actioncontext]) VALUES ('{0}','{1}')", strReason, val.ToString());
                //Database.ExecuteNonQuery(CommandType.Text, sql);
            }
        }


        private static object syncObj;
    }
}

